package com.billing.web.controller.archives;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Iterator;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.billing.model.entity.TUserEducation;
import com.billing.model.entity.TUserScan;
import com.billing.service.UserEducationService;
import com.billing.service.UserService;

@RequestMapping(value="/useredu")
@Controller
public class UserEducationController {
	@Resource
	private UserService userService;
	@Resource
	private UserEducationService userEducationService;
	
	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	
	@RequestMapping(value ="/saveEdu",method = RequestMethod.POST)
	@ResponseBody
	public String upload(MultipartFile mfile,
            HttpServletRequest request,HttpServletResponse response) throws IOException{
		int userid=(Integer) request.getSession().getAttribute("userid");
		System.out.println(userid);
		String graduationtime=request.getParameter("graduationtime");
		System.out.println(graduationtime);
		String universitie=request.getParameter("universitie");
		String major=request.getParameter("major");
		String educationNum=request.getParameter("educationNum");
		String academicdegreeNum=request.getParameter("academicdegreeNum");
		String educationPath="";
        String academicdegreePath="";
		String img="";
		String userScanpath="";
		// 将当前上下文初始化给 CommonsMutipartResolver （多部分解析器）
	    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
	    // 检查form中是否有enctype="multipart/form-data"
	    if (multipartResolver.isMultipart(request)) {
	      // 将request变成多部分request
	      MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
	      // 获取multiRequest 中所有的文件名
	      Iterator<String> iter = multiRequest.getFileNames();
	      
	      while (iter.hasNext()) {
	        // 一次遍历所有文件
	        MultipartFile file = multiRequest.getFile(iter.next());
	        if (!file.isEmpty()) {
//	        	// 服务端保存文件的路径
		          String realpath = "upload";
		          String path = request.getServletContext().getRealPath(realpath);
		          File uploadFile = new File(path);
		          if (!uploadFile.exists()) {
		            // 当保存文件的目录不存在则创建
		            uploadFile.mkdirs();
		          }
		          path = uploadFile.getAbsolutePath() + "/" + file.getOriginalFilename();
		          userScanpath=realpath+"/"+file.getOriginalFilename();
		          System.out.println(path);
		          //System.out.println(file.getOriginalFilename()+"++++++++++++++++++++");
		          // 上传
		          file.transferTo(new File(uploadFile.getAbsolutePath(), file.getOriginalFilename()));
	        	
	           //保存路径到数据库
	           String name=file.getName();
	           
	           if("educationfile".equals(name)){
	        	   educationPath=realpath+"/"+file.getOriginalFilename();
	           }else if("academicdegreefile".equals(name)){
	        	   academicdegreePath=realpath+"/"+file.getOriginalFilename();
	           }
            }
	      }
	    }
	    TUserEducation  userEducation=new TUserEducation();
	    userEducation.setUserId(userid);
	    userEducation.setAcademicdegreeNum(academicdegreeNum);
	    userEducation.setAcademicdegreePath(academicdegreePath);
	    userEducation.setEducationNum(educationNum);
	    userEducation.setEducationPath(educationPath);
	    Long gtime;
		try {
			gtime = format.parse(graduationtime).getTime();
			userEducation.setGraduationtime(gtime);
		} catch (ParseException e) {
		}
		userEducation.setMajor(major);
		userEducation.setUniversitie(universitie);
		int fal=userEducationService.insertTUserEducation(userEducation);
	    if(fal>0){
	    	img="保存成功";
	    }else{
	    	img="保存失败22";
	    }
	    return img;
	}
}
